#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#define ElemType char*
typedef struct
{
	char **base;
	char **top;
	int stacksize;
}SqStack;
SqStack InitStack(SqStack *S,int maxsize)
{
	int i;
	
	S->base=(char**)malloc(sizeof(char*)*maxsize);//(sizeof(ElemType)*maxsize);
	for(i=0;i<maxsize;i++)
	{
		*(S->base+i)=(char*)malloc(20);
		*(S->base+i)="";
	}
	if(!S->base)
	{
		printf("malloc failure");
		exit(0);
	}
	S->top = S->base;
	S->stacksize = maxsize;
	return *S;
}
SqStack Push(SqStack *S,char *e)
{	
	int i;
	if(S->top-S->base>=S->stacksize)
	{
		printf("stack overflow");
		exit(0);
	}
	//printf("%s\n",e);
	*S->top=e;
	(S->top)++;
	for(i=0;i<(S->top-S->base);i++)
	printf("%s",*(S->base+i));
	printf("\n");
//	printf("%s\n",*(S->top-1));
//	if(S->top-S->base>=2)
//	printf("a%s\n",*(S->top-2));
	return *S;
}
void Pop(SqStack *S,char **e)
{
	if(S->top==S->base)
	{
		printf("stack empty");
		exit(0);
	}
//	e=--S->top;
	(S->top)--;
//	printf("%s\n",*(S->top));

}
int main()
{
	SqStack dirt_name;
	dirt_name=InitStack(&dirt_name,10);
	char this_name[20]=".",father_name[20]="..";
	char *name="aaaa";
	struct dirent *this_dir=NULL,*father_dir=NULL,*temp=NULL;
	DIR *dir=NULL;
	int i;
	unsigned long int this_ino;
	while(1)
	{
		dir = opendir(this_name);
		if(dir == NULL)
		printf("error open");
		while(1)
		{
			this_dir = readdir(dir);
			if(strcmp(this_dir->d_name,".")==0)
			break;
		}
		this_ino=this_dir->d_ino;
		closedir(dir);
//		printf("%s,%d ",this_dir->d_name,this_dir->d_ino);
		dir = opendir(father_name);
		
		//while(1)
		//{
			father_dir = readdir(dir);
			//if(father_dir->d_name[0]=='.'&&father_dir->d_name[1]=='.')
			//break;
		//}
		if(this_dir->d_ino==father_dir->d_ino)
		break;
		//printf("%s ",father_dir->d_name);
		i=0;
		closedir(dir);
		dir=opendir(father_name);
		//printf("%d",this_dir->d_ino);
		while((temp=readdir(dir))!=NULL)
		{	
			//opendir()

			//temp=readdir(dir);
			//printf(" ,%s, ",temp->d_name);
			
			// printf(" ,%d %d, ",temp->d_ino,++i);
			if(temp->d_ino==this_ino)
			{
				dirt_name=Push(&dirt_name,temp->d_name);
			//	printf("bbb%sbbb ",temp->d_name);
				break;
			}
		}
		closedir(dir);
		strcat(this_name,"/..");
		strcat(father_name,"/..");
	}
//	printf("%s",*(dirt_name.top-2));
	//printf("%d",dirt_name.top-dirt_name.base);
	while(dirt_name.top!=dirt_name.base)
	{
		bzero(this_name,20);
		Pop(&dirt_name,&name);
		//printf("%s/",name);	
	}
}
